from Game7 import main
from tkinter import *
import ttkbootstrap
#import re

'''设置好看的主题'''
style = ttkbootstrap.Style(theme='minty')
TOP6 = style.master

'''弹窗的封装'''
def popup(txt):
	win = Toplevel()
	win.geometry('300x100')
	lb1 = Label(win, text=txt, anchor='center', font=('微软雅黑', 20))
	lb1.grid_columnconfigure(0, weight=1)
	lb1.grid(row=0, column=0)

'''判断账号有没有被注册过'''
def is_true(txt1, txt2, pop):
	global data
	num = txt1.get()
	if num not in data:
		popup('注册成功！')
		pas = txt2.get()
		data[num] = [pas, 0]
		# 注册成功后把新的账号和密码放到文件中
		fo = open('E:\\gitee\\my_class_py\\恐龙快跑\\run_dinosuar\\data.txt', 'a')
		fo.write(num + '\n')
		fo.write(pas + '\n')
		fo.write('0' + '\n')
		fo.close()
		pop.destroy()
	else:
		popup('该用户已经被注册过了')

'''开始注册新账号'''
def new():
	# 先进行弹窗
	pop = Toplevel()
	pop.title('注册')
	lb1 = Label(pop, text='new number:')
	lb2 = Label(pop, text='new passwd:')
	txt1 = Entry(pop, width=30)
	txt2 = Entry(pop, width=30)
	btn = Button(pop, text='确认', command=lambda: is_true(txt1, txt2, pop))
	lb1.grid(row=0, column=0)
	txt1.grid(row=0, column=1)
	lb2.grid(row=1, column=0)
	txt2.grid(row=1, column=1)
	btn.grid(row=2, column=0, sticky=('N', 'S', 'E', 'W'))

'''关闭游戏'''
def close_window():
	global game
	game.destroy()

'''开始游戏'''
def start(name):
	highest_score = data[name][1]
	# 获取分数
	while True:
		flag, highest_score = main(highest_score)
		data[name][1] = highest_score  
		with open('E:\\gitee\\my_class_py\\恐龙快跑\\run_dinosuar\\data.txt', 'w') as f:
			for key in data:
				f.write(key + '\n')
				f.write(data[key][0] + '\n')
				f.write(str(data[key][1]) + '\n')
		if not flag: break
#print('start')

'''判断账号和密码有效性并进入'''
def enter():
	global data, txt1
	num = txt1.get()
	if num not in data:
		popup('当前用户不存在！')
	else:
		if txt2.get() != data[num][0]:
			popup('密码错误！')
		else:
			close_window()
			start(num)

'''开始进程'''
if __name__ == "__main__":
	'''进程开始就可以开始读取文件内的账号和密码'''
	data = {}
	fo = open('E:\\gitee\\my_class_py\\恐龙快跑\\run_dinosuar\\data.txt', 'r+')
	while True:
		number = fo.readline().strip()
		if not number:
			break
		passwd = fo.readline().strip()
		score = int(fo.readline().strip())
		data[number] = [passwd, score]
	fo.close()

	'''主要gui界面'''
	game = Tk()
	game.title('小恐龙快跑')
	game.geometry('400x200')

	# 主框架
	# 让整个窗口元素靠上对齐
	game.pack_propagate(0)
	lb1 = Label(game, text='小恐龙快跑', font=('微软雅黑', 20))
	lb1.grid(row=0, column=1, sticky=('N', 'S', 'E', 'W'))
	lb1.grid_columnconfigure(0, weight=1)
	
	'''文本框(账号+密码)'''
	lb2 = Label(game, text='账号:')
	lb3 = Label(game, text='密码:')
	txt1 = Entry(game, width=30, bd=2)
	txt2 = Entry(game, width=30)
	lb2.grid(row=2, column=0)
	txt1.grid(row=2, column=1)
	lb3.grid(row=3, column=0)
	txt2.grid(row=3, column=1)

	'''登录和注册按钮'''
	btn3 = Button(game, text='登录', command=enter)
	btn3.grid(row=4, column=0, sticky=('N', 'S', 'E', 'W'))
	btn4 = Button(game, text='注册', command=new)
	btn4.grid(row=4, column=2, sticky=('N', 'S', 'E', 'W'))

	btn2 = Button(game, text='QUIT', command=close_window)
	btn2.grid(row=4, column=1)

	# 展示窗口
	game.mainloop()

